<?php

namespace AdminBundle\Controller;

use AdminBundle\Entity\BaseWebchatMember;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use AdminBundle\Utility\Constant;

/**
 * Basewebchatmember controller.
 *
 * @Route("basewebchatmember")
 */
class BaseWebchatMemberController extends Controller
{
    /**
     * Lists all baseWebchatMember entities.
     *
     * @Route("/", name="basewebchatmember_index")
     * @Method("GET")
     */
    public function indexAction(Request $request)
    {
        $page = $request->get("page");
        $page = is_numeric($page) ? $page : 1;
        $keyword = $request->get('keyword');
        $date_range = $request->get('date_range');
        $wechat_user_id = $request->get('wechat_user_id');
        $device_group = $request->get('device_group');
        $device = $request->get('device');

        if (!empty($date_range)) {
            $date_range = explode(" - ", $date_range);
            $starttime = $date_range[0];
            $endtime = $date_range[1];
        } else {
            $starttime = $endtime = null;
        }

        $starttime = date("Y-m-d", strtotime($starttime ?: "-3 day"));
        $endtime = date("Y-m-d", strtotime($endtime ?: "+1 day"));

        $params = ['keyword' => $keyword, 'starttime' => $starttime, 'endtime' => $endtime,
            'device_group' => $device_group, 'device' => $device,
            'wechat_user_id' =>$wechat_user_id
        ];

        $result = $this->getDoctrine()->getRepository('AdminBundle:BaseWebchatMember')->pageQuery($params, $page);

        $em = $this->getDoctrine()->getManager();
        // 终端组
        $baseGroup = $em->getRepository('AdminBundle:BaseGroup')->findAll();
        $params['baseGroups'] = $baseGroup;
        // 终端
        $baseDevice = $em->getRepository('AdminBundle:BaseDevice')->findAll();
        $params['baseDevices'] = $baseDevice;

        return $this->render('AdminBundle:basewebchatmember:index.html.twig', array_merge([
            'dataSources' => $result['list'],
            'querystring' => http_build_query($params),
            'page' => $result['page'],
        ], $params));
    }

    /**
     * Finds and displays a baseWebchatMember entity.
     *
     * @Route("/{id}", name="basewebchatmember_show")
     * @Method("GET")
     */
    public function showAction(BaseWebchatMember $baseWebchatMember)
    {

        return $this->render('AdminBundle:basewebchatmember:show.html.twig', array(
            'baseWebchatMember' => $baseWebchatMember,
        ));
    }
}
